var util = require("../../utils/util.js");
Page({
  data: {
    gid: '',
    money: '',
    uid: '',
    imgUrl: '',
    title: '',
    unit: '',
    number: 1,
    tariff: '',
    username: '',
    newPhone: '',
    newSlet: '',
    newName: '',
    email: '',
    linkMan: '',
    linkPhone: '',
    detailAddress: '',
    boxWhether: "",
    coupon: '',
    cid: '',
    region:'',
    addressId:''
  },
  onLoad: function (options) {
    var that = this;
    that.setData({
      gid: options.gid,
      uid: options.uid,
      phone: options.phone,
      boxWhether: "是否使用优惠券",
    })
    wx.request({
      url: "http://1.aiym.applinzi.com/aiyouwai.php",
      data: {
        type: "getGoodsInfo",
        paras: {
          gid: options.gid
        }
      },
      header: { "content-type": "application/json" },
      success: function (res) {
        console.log(res)
        that.setData({
          imgUrl: res.data[0].imgUrl,
          title: res.data[0].title,
          unit: res.data[0].unit,
          money: res.data[0].price,
          tariff: res.data[0].price
        })
      }
    })
  },

  // 设置加减确认服务数量
  add: function () {
    var that = this;
    var number1 = that.data.number + 1;
    that.setData({
      number: number1,
      tariff: number1 * that.data.money
    })
  },
// 点击设置减后的数量
  subtract: function () {
    var that = this;
    var number1 = that.data.number - 1;
    if (number1 < 1) {
      number1 = 1;
    }
    var money = 0;
    if (that.data.tariff = this.data.money) {
      that.data.tariff = this.data.money * 2;
    }
    that.setData({
      number: number1,
      tariff: that.data.tariff - that.data.money
    })
  },

// 添加服务信息value值
  article: function (e) {
    var that = this;
    that.setData({
      article: e.detail.value
    })
  },
  // 点击跳转到地址管理页面
  addSlet: function () {
    wx.navigateTo({
      url: '../address/address',
    })
  },
  // 点击添加优惠券页面
  select: function () {
    wx.navigateTo({
      url: '../coupons/coupons',
    })
  },
  //提交将数据填入数据库
  submit: function () {
    var that = this;
    console.log(this.data.linkMan)
    if (this.data.linkMan != '') {
      wx.request({
        url: "http://1.aiym.applinzi.com/aiyouwai.php",
        data: {
          type: "publish",
          paras: {
            uid: this.data.uid.uid,
            gid: this.data.gid,
            isCoupon: this.data.coupon,
            couponId: this.data.cid,
            price: this.data.money,
            elsemsg: this.data.article,
            phoneNum: this.data.linkPhone,
            address: this.data.detailAddress,
            linkman: this.data.linkMan,
            goodNum: this.data.number,
            region:this.data.region,
            aid:this.data.addressId
          }
        },
        header: { "content-type": "application/json" },
        success: function (res) {
          console.log(res.data);
          // 订单成功之后跳转到等待页面
          wx.navigateTo({
            url: '../succeed/succeed?uid=' + that.data.uid.uid + "&oid=" + res.data.oid,
          })
        }
      })
    } else {
      wx.showModal({
        content: '请添加服务信息',
        showCancel: false,
        success: function (res) {
        }
      });
    }
  },
  onShow: function (options) {
    var that = this;
    console.log(that.data.tariff)
    wx.getStorage({
      key: 'key',
      success: function (res) {
        console.log(res)
        if (res.data != '') {
          that.setData({
            boxWhether: res.data.unit + '元',
            coupon: res.data.unit,
            cid: res.data.cid
          })
        }
      },
    })
    // 页面显示
    wx.getStorage({
      key: 'userInfo',
      success: function (res) {
        console.log(res.data)
        that.setData({
          uid: res.data
        })
wx.request({
          url: "http://1.aiym.applinzi.com/aiyouwai.php",
          data: {
            type: "getUserStaticAddress",
            paras: {
              uid: res.data.uid
            }
          },
          header: { "content-type": "application/json" },
          success: function (res) {
            console.log(res.data)
            if (res.data != null) {
              that.setData({
                linkMan: res.data[0].linkMan,
                linkPhone: res.data[0].linkPhone,
                detailAddress: res.data[0].detailAddress,
                addressId:res.data[0].address_id,
                region:res.data[0].region
              })
            }
          }
        })
      },
    })    
  },
})